$(document).ready(function () {
  /**
   * @description: 预加载
   * @param {*}
   * @return {*}
   */
  var storage = window.localStorage
  var RSUId = storage.RSUId
  $.ajax({
    url: '../../rsu/getRsuMsg/'+RSUId,
    type: 'GET', //GET
    // data: {
    //   rsuId: RSUId
    // },
    success: function (response) {
      var html = ''
      for (var i = 0; i < response.data.msgs.length; i++) {
        html += `<tr><td>${response.data.msgs[i].msgId}</td>
            <td>${response.data.msgs[i].msgDetail}</td>
            <td>${response.data.msgs[i].msgType}</td>
            <td>${response.data.msgs[i].msgTime}</td>
            <td>${response.data.msgs[i].encrypted}</td>
            <td>
            <button id='${response.data.msgs[i].msgId}' onclick = 'deleteRSUMsg(this)' type="button" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
            </button> </td>
            `
      }
      // for (var i = 0; i < response.data.msgs.length; i++) {
      //   let vehicle_key_re = response.data.msgs[i].encrypted.slice(0, 8);
      //   vehicle_key_re = vehicle_key_re + "...";
      //   html += `<tr><td>${response.data.msgs[i].msgId}</td>
      //       <td>${response.data.msgs[i].msgDetail}</td>
      //       <td>${response.data.msgs[i].msgType}</td>
      //       <td>${response.data.msgs[i].msgTime}</td>
      //       <td>${vehicle_key_re}</td>
      //       <td>
      //       <button id='${response.data[i].id}' onclick = 'deleteRSUMsg(this)' type="button" class="btn btn-danger">
      //       <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
      //       </button> </td>
      //       `
      // }
      $('#RSUMsgTableT').append(html)
    },
    error: function (error) {
      response = {
        data: [
          {
            id: 1,
            msgDetail: '两车相撞，道路堵塞（测试数据，程序异常时出现！）',
            msgType: 'A',
            msgTime: '2020/7/19',
            msgStrategy: '不允通行'
          }
        ]
      }
      var html = ''
      for (var i = 0; i < response.data.length; i++) {
        let vehicle_key_re = response.data[i].encrypted.slice(0, 8);
        vehicle_key_re = vehicle_key_re + "...";
        html += `<tr><td>${response.data[i].id}</td> 
            <td>${response.data[i].msgDetail}</td> 
            <td>${response.data[i].msgType}</td> 
            <td>${response.data[i].msgTime}</td> 
            <td>${vehicle_key_re}</td> 
            <td>
            <button id='${response.data[i].id}' onclick = 'deleteRSUMsg(this)' type="button" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
            </button> </td>
            `
      }
      // for (var i = 0; i < response.data.length; i++) {
      //   html += `<tr><td>${response.data[i].id}</td>
      //       <td>${response.data[i].msgDetail}</td>
      //       <td>${response.data[i].msgType}</td>
      //       <td>${response.data[i].msgTime}</td>
      //       <td>${response.data[i].encrypted}</td>
      //       <td>
      //       <button id='${response.data[i].id}' onclick = 'deleteRSUMsg(this)' type="button" class="btn btn-danger">
      //       <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
      //       </button> </td>
      //       `
      // }
      $('#RSUMsgTableT').append(html)
    }
  })

   /**
   * @description: 删除信息
   * @param {*}
   * @return {*}
   */
  deleteRSUMsg = function (tr) {
    $.ajax({
      url: '../../msg/removemsg',
      type: 'POST',
      data: {
        MsgId: tr.id
      },
      success: function (response) {
        hulla.send(response.msg, 'success')
        window.location.reload();
      },
      error: function (error) {
          hulla.send(response.msg, 'danger')
      }
    })
  }

   /**
   * @description: 添加信息
   * @param {*}
   * @return {*}
   */
  addRSUMsgBtn = function () {
    var msgDetail = $('#inputMsgDetail').val()
    var msgType = $('#inputMsgType').val()
    var msgTime = $('#inputMsgTime').val()
    var msgStrategy = $('#inputMsgStrategy').val()
    if (msgDetail == '') {
      $('#msgDetailForm').addClass('has-error')
      $('#msgDetailIptDiv').append(
        " <span class='help-block'>不可为空</span>"
      )
      setTimeout(() => {
        $('#msgDetailForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (msgType == '') {
      $('#msgTypeForm').addClass('has-error')
      $('#msgTypeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#msgTypeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (msgTime == '') {
      $('#msgTimeForm').addClass('has-error')
      $('#msgTimeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#msgTimeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (msgStrategy == '') {
      $('#msgStrategyForm').addClass('has-error')
      $('#msgStrategyIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#msgStrategyForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (msgDetail != '' && msgType != '' && msgTime != '' && msgStrategy != '') {
      $.ajax({
        url: '../../msg/addmsg', //引入地址
        type: 'POST', //请求方式
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(  {
          rsu_id:RSUId,
          msg_detail: msgDetail,
          msg_type: msgType,
          msg_time: msgTime,
          msg_strategy: msgStrategy
        }),
        success: function (response) {
          $('.help-block').remove()
          if (response.isSuccess) {
            hulla.send(response.msg, 'success')
            window.location.reload()
          }
        },
        error: function (response) {
          hulla.send(response.msg, 'danger')
        }
      })
    }
  }
})
